package com.cbank.dao;

import com.cbank.domain.BaseEntity;
import com.cbank.domain.ModuleShow;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ModuleShowDao {
    public static final String ALL="id,view_id,create_time,update_time,module_id,type,title,picture_url,source,banner_url,news_type,publish_time,source_url,en_title";

    @Select("select "+ALL+" from module_show where id = #{id}")
    ModuleShow getById(Integer id);

    @Select("select "+ALL+" from module_show where view_id = #{viewId}")
    ModuleShow getByViewId(String viewId);

    @Select("select "+ALL+" from module_show where module_id = #{moduleId} and status = "+ BaseEntity.STATUS_VALID)
    List<ModuleShow> getListByModuleId(Integer moduleId);

    @Select("select "+ALL+" from module_show where module_id = #{moduleId} and status = "+BaseEntity.STATUS_VALID+" and type = #{type}")
    List<ModuleShow> getListByModuleIdAndType(@Param(value = "moduleId")Integer moduleId,@Param(value = "type")Integer type);

    @Insert("insert into module_show (view_id,module_id,type,title,picture_url,source,banner_url,news_type,publish_time,source_url,en_title) " +
            "values(#{viewId},#{moduleId},#{type},#{title},#{pictureUrl},#{source},#{bannerUrl},#{newsType},#{publishTime},#{sourceUrl},#{enTitle})")
    void insert(ModuleShow moduleShow);

    @Update("update module_show set view_id=#{viewId},create_time=#{createTime},update_time=#{updateTime},module_id=#{moduleId},type=#{type},title=#{title}," +
            "picture_url=#{pictureUrl},source=#{source},banner_url=#{bannerUrl},news_type=#{newsType},publish_time=#{publishTime},source_url=#{sourceUrl},en_title=#{enTitle} where id = #{id}")
    void update(ModuleShow moduleShow);

    @Update("update module_show set status = #{status} where id = #{moduleShowId}")
    void updateStatus(@Param(value = "status")Integer status,@Param(value = "moduleShowId")Integer moduleShowId);

    /**
     * 新闻专用 查询日期分组
     * @param moduleId
     * @return
     */
    @Select("SELECT DATE_FORMAT(publish_time,'%Y-%m') AS DAYS FROM module_show WHERE module_id=#{moduleId} and status = "+ BaseEntity.STATUS_VALID+"  GROUP BY DAYS ")
    List<String> getAllDate(@Param(value = "moduleId")Integer moduleId);

    @Select("SELECT "+ALL+" FROM module_show WHERE publish_time LIKE CONCAT('%',#{publishTime},'%') and module_id=#{moduleId}")
    List<ModuleShow> getAllByDate(@Param(value = "publishTime")String publishTime,@Param(value = "moduleId")Integer moduleId);
}
